package com.kexuexiong.clientapi.controller;

import cn.hutool.core.io.FileUtil;
import com.kexuexiong.clientapi.services.IFileService;
import com.kexuexiong.file.oss.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>功能描述</p>
 *
 * @author xy
 * @version 1.0
 * @ClassName FileController.java
 * @date 2021年12月2021/12/1日 16:03
 */
@RestController
@RequestMapping("file/")
@CrossOrigin
public class FileController {

    @Autowired
    private OssService ossService;


    @PostMapping(value = "uploadFile")
    public Object uploadFile(){
        ossService.uploadFile();
        return "成功！";
    }

    @PostMapping("fileList")
    public Object getFileList() throws IOException {
        List<Map<String, Object>> excelDataListPage = ossService.getExcelDataListPage("copy-322550e9-2332-419f-b295-8116d58bfa7a", 1, 10);
        return excelDataListPage;
    }

    @Autowired
    private IFileService iFileService;

    @PostMapping("uploadFile2OSS")
    public void uploadFile2OSS(@RequestParam("file") MultipartFile file) throws IOException {
        //保存到本地
        String dest = "D:\\downloadFile\\" +file.getOriginalFilename();
        File fileDest = new File(dest);
        file.transferTo(fileDest);
        //上传oss
        String fileKey = ossService.uploadFileByFilePath(dest, file.getOriginalFilename());
        //保存数据库
        com.kexuexiong.common.entity.File filePo = new com.kexuexiong.common.entity.File();
        filePo.setFileKey(fileKey);
        filePo.setIsDel(false);
        filePo.setCreateDate(LocalDateTime.now());
        filePo.setOssBucket("fresh-fruit");
        iFileService.save(filePo);
        //删掉本地的
        fileDest.delete();
    }
}
